package com.baox.agridata.synchronous.schedule;


import com.baox.agridata.synchronous.assembly.SlaughteringCompanyAssembly;
import com.baox.agridata.synchronous.constant.Constant;
import com.baox.agridata.synchronous.constant.TaskNameConstant;
import com.baox.agridata.synchronous.entity.quanguo.*;
import com.baox.agridata.synchronous.entity.shanxi.SxAgricultureTask;
import com.baox.agridata.synchronous.entity.shanxi.TBASEDesignatedPigSlaughteringCompany;
import com.baox.agridata.synchronous.service.quanguo.*;
import com.baox.agridata.synchronous.service.shanxi.*;
import com.baox.agridata.synchronous.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.*;

/**
 * 票证外的农业部推送数据在这
 */

@Slf4j
@Component
@Configuration
public class LocalPushDataToAgriculturalOtherInfo {


    @Autowired
    private SxAgricultureTaskService sxAgricultureTaskService;

    @Autowired
    private McSzqyxxService mcSzqyxxService;

    @Autowired
    private McJczsService mcJczsService;

    @Autowired
    private McJczsAttService mcJczsAttService;


    @Autowired
    private McFyztxxsService mcFyztxxsService;


    @Autowired
    private McQyxxsService mcQyxxsService;

    @Autowired
    private McGrxxsService mcGrxxsService;

    @Autowired
    private JczGzjlsService jczGzjlsService;


    @Autowired
    private TBASEDesignatedPigSlaughteringCompanyService tbaseDesignatedPigSlaughteringCompanyService;

    @Autowired
    private TUserCheckPointService tUserCheckPointService;  //检查站和检查站附表信息都查的是这个表

    @Autowired
    private TXDRTrafficService txdrTrafficService;   //贩运主体


    @Autowired
    private TXDRVeterinarySocialOrganizationService txdrVeterinarySocialOrganizationService;  //兽医社会化服务组织备案


    @Autowired
    private TXDRLivestockMarketService txdrLivestockMarketService;  //活畜禽交易市场


    @Autowired
    private TXDRIsolationService txdrIsolationService;  //动物隔离场


    @Autowired
    private TXDRSlaughterHouseService txdrSlaughterHouseService;  //屠宰场企业


    @Autowired
    private TXDRFarmService txdrFarmService;   //养殖企业数据

    @Autowired
    private TCheckPointCheckResultService tCheckPointCheckResultService;  //过站记录数据

    @Autowired
    private SlaughteringCompanyAssembly slaughteringCompanyAssembly;


    @Autowired
    private SplitDataSummary splitDataSummary;


    @Value("${DbConfig.FARMGRStartTime}")
    private String FARMGRStartTime;

    @Value("${DbConfig.JCZ_GZJLS}")
    private String jczGzjlsStartTime;   //指定通道过站记录表,时间




    /**
     * 机构人员字段（检查站）
     */
    private String[] JczsComparFileds = new String[]{"jb", "qhdm", "mc", "dh", "fzr", "dz", "zt"};

    /**
     * 机构人员字段（检查站附表信息）
     */
    private String[] JczsAttComparFileds = new String[]{"bh", "glmc", "rs", "xldq"};

    /**
     * 贩运主体的字段
     */
    private String[] FyztxxsComparFileds = new String[]{"fyrlx", "fyrzl", "fyrmc", "fyrbh", "fyrhm", "fyrdh", "qhdm", "fyrdz", "fydw"};

    /**
     * 企业信息表的字段比对
     */
    private String[] QyxxsComparFileds = new String[]{"qybm", "xydm", "qhdm", "mc", "dh", "fzr", "dz"};

    /**
     * 个人基本信息
     */
    private String[] GRxxsComparFileds = new String[]{"dwdm", "ztdm", "sfzh", "xm", "dh", "qhdm", "dz"};


    private static final String JCJG0 = "0";
    private static final String JCJG1 = "1";


    /**
     * 国家已公布生猪屠宰企业信息接收(全国生猪定点屠宰场的数据接收)
     */
    @Scheduled(cron = "0 59 23 * * ?")
    public void PigSlaughteringCompanyTask() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.PigSlaughteringCompanyTask);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("【生猪定点屠宰场任务开始执行】");
            try {
                List<McSzqyxx> l_list = mcSzqyxxService.getDesignatedPigSlaughteringCompanyList();
                if (l_list != null && l_list.stream().count() > 0) {
                    Integer addCount = 0, updateCount = 0, deleteCount = 0;
                    List<TBASEDesignatedPigSlaughteringCompany> addList = new ArrayList<>();
                    for (McSzqyxx mcSzqyxx : l_list) {
                        Integer isExist = tbaseDesignatedPigSlaughteringCompanyService.checkBASEDesignatedPigSlaughteringCompanyIsExist(mcSzqyxx.getId());
                        if (isExist == null || isExist.intValue() == 0) {
                            TBASEDesignatedPigSlaughteringCompany slaughteringCompany = slaughteringCompanyAssembly.getSlaughteringCompany(mcSzqyxx);
                            addList.add(slaughteringCompany);
                        } else {
                            //需要更新的数据
                            updateCount += tbaseDesignatedPigSlaughteringCompanyService.updateTBASEDesignatedPigSlaughteringCompany(mcSzqyxx);
                        }
                    }
                    if (addList.size() > 0)  //如果新增集合不为空，进行批量插入数据
                        addCount = tbaseDesignatedPigSlaughteringCompanyService.batchInsertDesignatedPigSlaughteringCompany(addList);
                    if ((addCount.intValue() + updateCount.intValue()) == l_list.size())  //如果更新和新增的数据等于本次查询数据，则进行删除数据
                    {
                        log.info("数量一致，执行删除");
                        deleteCount = mcSzqyxxService.deleteMcSzqyxx(l_list);
                    }
                    log.info("【国家已公布生猪屠宰企业】信息接收: 农业部中心数据条目数：{}，本地插入数据条目数：{}，本地更新数据条目数：{},农业部中心数据删除条目数：{}", l_list.size(), addCount, updateCount, deleteCount);
                } else {
                    log.info("【国家已公布生猪屠宰企业】本次未查询到数据");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【生猪定点屠宰场任务未开启】");
        }
    }

    /**
     * 陕西发送到前置机数据，指定通道过站记录数据
     */
    @Scheduled(cron = "0 58 23 * * ?")
    public void synchronizationJczGzjls() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.synchronizationJczGzjls);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("【指定通道,过站记录数据】定时任务数据同步");
            try {
                FileUtil l_file = new FileUtil();
                String l_filePath = l_file.getPath() + File.separator + "TaskConfig" + File.separator + "ZDTDJL";
                String l_fileName = "StartTimeConfig.txt";
                ///读取最后一次同步数据的时间
                if (l_file.CheckFileExists(l_filePath, l_fileName))
                    jczGzjlsStartTime = l_file.readLastLineV2(l_filePath, l_fileName);

                // 写入当前时间
                String endTime = DateUtil.dateToMinute(new Date()) + ":00";
                List<JczGzjls> jczGzjls = tCheckPointCheckResultService.selectJczGzjlsData(jczGzjlsStartTime, endTime);


                if (jczGzjls != null && jczGzjls.size() > 0) {
                    List<JczGzjls> list = new ArrayList<>();
                    Integer insertSuccess = 0;
                    for (JczGzjls gzjls : jczGzjls) {
                        //第一个处理的字段dytdm(动物用途)
                        if (StringUtil.isNotEmpty(gzjls.getDytdm())) {
                            String _Dytdm = DicDataTrun.PurposeSystemConvertCountry(gzjls.getDytdm());
                            gzjls.setDytdm(_Dytdm);
                        } else gzjls.setDytdm("--");
                        //第二个处理的字段sldw(数量单位)
                        if (StringUtil.isNotEmpty(gzjls.getSldw())) {
                            String _Sldw = DicDataTrun.UnitSystemConvertCountry(gzjls.getSldw());
                            gzjls.setSldw(_Sldw);
                        }


                        //车牌号查询结果0未备案1已备案2未检查.动物证必须检查  jcph
                        if (gzjls.getJcjg().equals(JCJG0) && gzjls.getJcph().equals("-1")) {
                            gzjls.setJcph(JCJG0); //如果检查结果为0，车牌查询结果为-1时，将查询结果设置为0
                        } else if (gzjls.getJcjg().equals(JCJG1) && gzjls.getJcph().equals("-1") && gzjls.getPhyz().equals(JCJG1)) {
                            gzjls.setJcph(JCJG1);   //如果检查结果为1，车牌查询结果为-1，检查车牌是否一致，则将查询结果设置为1
                        } else if (gzjls.getJcjg().equals(JCJG1) && gzjls.getJcph().equals("-1") && gzjls.getPhyz().equals(JCJG0)) {
                            gzjls.setJcph(JCJG0);   //检查结果是1，车牌查询结果为-1.检查车牌为0即不一致，那么jcph设置为0，车牌号不一致
                        }


                        //承运人查询结果 0未备案1已备案2未检查动物证必须检查 jcys
                        if (gzjls.getJcjg().equals(JCJG0) && gzjls.getJcys().equals("-1")) {
                            gzjls.setJcys(JCJG0); //如果检查结果为0，承运人查询结果为-1时，将查询结果设置为0
                        } else if (gzjls.getJcjg().equals(JCJG1) && gzjls.getJcys().equals("-1") && gzjls.getJcysyz().equals(JCJG1)) {
                            gzjls.setJcys(JCJG1);   //如果检查结果为0，承运人查询结果为-1时，检查承运人是否与检疫证一致，则将承运人查询结果设置为1
                        }

                        //实际车牌号查询结果  0未备案1已备案2未检查。动物证必须检查
                        if (StringUtil.isNotEmpty(gzjls.getJcsjph()) && gzjls.getJcsjph().equals("-1") && gzjls.getPhyz().equals(JCJG1)) {
                            gzjls.setJcsjph(null);  //如果字段不为空，且等于-1，并且车牌号一致，将值设置为null
                        }

                        //实际承运人查询结果0未备案1已备案2未检查。动物证必须检查
                        if (StringUtil.isNotEmpty(gzjls.getJcsjys()) && StringUtil.isNotEmpty(gzjls.getJcysyz()) && gzjls.getJcysyz().equals(JCJG1) && gzjls.getJcsjys().equals("-1")) {
                            gzjls.setJcsjys(null);   //如果实际承运人查询结果不为空，并且承运人是否不一致结果为1，则实际承运人查询结果为-1，则将值设置为null
                        }

                        if (StringUtil.isNotEmpty(gzjls.getJcdebh()) && gzjls.getJcdebh().equals("-1")) {
                            gzjls.setJcdebh(null);
                        }

                        list.add(gzjls);
                    }
                    if (list.size() > 0) insertSuccess = jczGzjlsService.batchInsertJczGzjls(list);
                    log.info("【指定通道,过站记录数据】，审核记录数据发送完成，本期时间节点为:{},同步数据总条目为：{},成功插入数据库的条目数：{}", jczGzjlsStartTime, list.size(), insertSuccess);
                } else {
                    log.info("【指定通道,过站记录数据】,本期未查询到数据，开始时间节点为:{},下期开始时间节点为:{},同步数据总条目为：{}", jczGzjlsStartTime, endTime, jczGzjls.size());
                }
                l_file.WriteContent(endTime, l_filePath, l_fileName); //如果说该时间段内，没查到数据，也将时间进行更新
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【指定通道,过站记录数据】定时任务未开启");
        }
    }

    /**
     * 机构定时同步(检查站) (每日23点55分执行)
     */
    @Scheduled(cron = "0 55 23 * * ?")
    public void CheckPointTask() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.CheckPointTask);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("开始【检查站】定时数据同步");
            try {
                List<McJczs> l_localList = tUserCheckPointService.selectCheckPointInfo();  //查询本省的检查站信息
                List<McJczs> l_centerList = mcJczsService.getCenterAgencyList();   //查询农业部的所以检查站信息
                List<McJczs> insertLists = new ArrayList<>();
                List<McJczs> updateLists = new ArrayList<>();
                if (l_localList != null && l_localList.size() > 0) {
                    for (McJczs entity : l_localList) {  //循环本省的检查站信息
                        Optional<McJczs> jcqh = l_centerList.stream().filter((McJczs bean) -> bean.getId().equals(entity.getId())).findFirst();  //
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), JczsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) mcJczsService.batchInsertMcJczs(insertLists);
                if (updateLists.size() > 0) mcJczsService.batchUpdateMcJczs(updateLists);
                log.info("【检查站】定时同步任务:省级平台：" + l_localList.size() + "；农业部：" + l_centerList.size() + "；更新：" + updateLists.size() + "；新增：" + insertLists.size());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            log.info("【检查站】定时数据同步任务未开启");
        }
    }

    /**
     * 机构定时同步(检查站附表信息)(每日23点50分执行)
     */
    @Scheduled(cron = "0 50 23 * * ?")
    public void synchronizationMcJczsAtt() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.synchronizationMcJczsatt);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("开始【检查站附表信息】定时数据同步");
            try {
                List<McJczsatt> l_localList = tUserCheckPointService.selectMcJczsatt();  //查询本省的检查站附表信息
                List<McJczsatt> l_centerList = mcJczsAttService.getJczsAttInfos();   //查询农业部的检查站附表信息
                List<McJczsatt> insertLists = new ArrayList<>();
                List<McJczsatt> updateLists = new ArrayList<>();
                if (l_localList != null && l_localList.size() > 0) {
                    for (McJczsatt entity : l_localList) {
                        Optional<McJczsatt> jcqh = l_centerList.stream().filter((McJczsatt bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), JczsAttComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) mcJczsAttService.batchInsertMcJczsatt(insertLists);
                if (updateLists.size() > 0) mcJczsAttService.batchUpdateMcJczsAtt(updateLists);
                log.info("【检查站附表信息】定时同步任务:省级平台：" + l_localList.size() + "；农业部：" + l_centerList.size() + "；更新：" + updateLists.size() + "；新增：" + insertLists.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【检查站附表信息】定时数据同步任务未开启");
        }
    }

    /**
     * 贩运户同步 (每日23点45分执行)
     */
    @Scheduled(cron = "0 45 23 * * ?")
    public void TrafficAsyncTask() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.TrafficAsyncTask);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("开始【贩运户】定时数据同步");
            try {
                List<Mcfyztxxs> l_localList = txdrTrafficService.getTrafficUserList();  //查询本省的贩运户数据
                List<Mcfyztxxs> l_centerList = mcFyztxxsService.getMcFyztxxsList();  //查询农业部的数据
                List<Mcfyztxxs> insertLists = new ArrayList<>();
                List<Mcfyztxxs> updateLists = new ArrayList<>();
                Integer addResult = 0, updateResult = 0;
                if (l_localList != null && l_localList.size() > 0) {
                    for (Mcfyztxxs entity : l_localList) {
                        Optional<Mcfyztxxs> jcqh = l_centerList.stream().filter((Mcfyztxxs bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), FyztxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) addResult = splitDataSummary.interceptListMcFyztxxs(insertLists);
                if (updateLists.size() > 0) updateResult += mcFyztxxsService.batchUpdateMcFyztxxs(updateLists);
                log.info("【贩运户】数据初始化结束:省级平台：" + l_localList.size() + ";农业部数据：" + l_centerList.size() + ";更新：" + updateLists.size() + ";" + updateResult + "新增：" + insertLists.size() + ";" + addResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【贩运户】定时数据同步任务未开启");
        }
    }

    /**
     * 本省企业信息发送(养殖场企业数据)(每日23点40分执行)
     */
    @Scheduled(cron = "0 40 23 * * ?")
    public void FarmCompanyTask() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.FarmCompanyTask);
        if (sxAgricultureTask != null && sxAgricultureTask.getZt().intValue() == 1) {
            log.info("开始【养殖企业】定时数据同步");
            try {
                List<McQyxxs> l_localList = txdrFarmService.selectCompanyFarm();
                List<McQyxxs> l_centerList = mcQyxxsService.getCenterQyxxsList(Constant.MC_QYLX_XDR_FARM);
                List<McQyxxs> insertLists = new ArrayList<>();
                List<McQyxxs> updateLists = new ArrayList<>();
                Integer addResult = 0, updateResult = 0;
                if (l_localList != null && l_localList.size() > 0) {
                    for (McQyxxs entity : l_localList) {
                        Optional<McQyxxs> jcqh = l_centerList.stream().filter((McQyxxs bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), QyxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) addResult = splitDataSummary.interceptListFarmCompany(insertLists);
                if (updateLists.size() > 0) updateResult += mcQyxxsService.batchUpdateMcQyxxs(updateLists);
                log.info("【养殖企业】数据初始化结束:省级平台：" + l_localList.size() + ";农业部数据：" + l_centerList.size() + ";更新：" + updateLists.size() + ";" + updateResult + "新增：" + insertLists.size() + ";" + addResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【养殖企业】定时数据同步任务未开启");
        }
    }

    /**
     * 本省个人信息发送(个人类型的数据)(每日23点35分执行)
     */
    @Scheduled(cron = "0 35 23 * * ?")
    public void FarmPersonTask() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.FarmCompanyTask);
        if (sxAgricultureTask != null && sxAgricultureTask.getZt().intValue() == 1) {
            log.info("开始【养殖场个人】定时数据同步");
            try {
                FileUtil l_file = new FileUtil();
                String l_filePath = l_file.getPath() + File.separator + "TaskConfig" + File.separator + "FarmPerson";
                String l_fileName = "StartTimeConfig.txt";
                ///读取最后一次同步数据的时间
                if (l_file.CheckFileExists(l_filePath, l_fileName)) {
                    FARMGRStartTime = l_file.readLastLineV2(l_filePath, l_fileName);
                }
                String endTime = DateUtil.dateToMinute(new Date()) + ":00";
                List<McGrxxs> l_localList = txdrFarmService.selectPersonFarm(FARMGRStartTime);
                if (l_localList != null && l_localList.size() > 0) {
                    List<McGrxxs> insertLists = new ArrayList<>();
                    List<McGrxxs> updateLists = new ArrayList<>();
                    Integer addResult = 0, updateResult = 0;
                    for (McGrxxs entity : l_localList) {   //循环本次更新的数据
                        McGrxxs centerMcEntity = mcGrxxsService.selectGrxxsById(entity.getId());   //判断这条数据是否已经推送过农业部
                        if (centerMcEntity != null) {  //农业部库里存在这条数据
                            Map compareResult = CommonUtil.compareFields(entity, centerMcEntity, GRxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                    if (insertLists.size() > 0) addResult = splitDataSummary.interceptListFarmPerson(insertLists);
                    if (updateLists.size() > 0) updateResult += mcGrxxsService.batchUpdateMcGrxxs(updateLists);
                    log.info("相对人养殖场个人数据初始化结束:省级平台：" + l_localList.size() + ";更新：" + updateLists.size() + "," + updateResult + ";新增：" + insertLists.size() + "," + addResult);
                } else {
                    log.info("相对人养殖场个人数据本次未查询到数据");
                }
                l_file.WriteContent(endTime, l_filePath, l_fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【养殖场个人】定时数据同步任务未开启");
        }
    }








    //-----------============每月一个周期==============-------------------

    /**
     * 兽医社会化服务组织 (每月1日凌晨30分执行)
     */
    @Scheduled(cron = "0 30 0 1 * ?")
    public void VeterinarySocialOrganizationToDepartment() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.VeterinarySocialOrganization);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("【兽医社会化服务组织】备案定时数据开始同步");
            try {
                List<McQyxxs> l_localList = txdrVeterinarySocialOrganizationService.selectVeterinarySocialOrganization();
                List<McQyxxs> l_centerList = mcQyxxsService.getCenterQyxxsList(Constant.MC_QYLX_SOCIAL_SERVICE_ORGANIZATION);
                List<McQyxxs> insertLists = new ArrayList<>();
                List<McQyxxs> updateLists = new ArrayList<>();
                Integer addResult = 0, updateResult = 0;
                if (l_localList != null && l_localList.size() > 0) {
                    for (McQyxxs entity : l_localList) {
                        Optional<McQyxxs> jcqh = l_centerList.stream().filter((McQyxxs bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), QyxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) addResult = mcQyxxsService.batchInsertMcQyxxs(insertLists);
                if (updateLists.size() > 0) updateResult += mcQyxxsService.batchUpdateMcQyxxs(updateLists);
                log.info("【兽医社会化服务组织】数据初始化结束:省级平台：" + l_localList.size() + ";农业部数据：" + l_centerList.size() + ";更新：" + updateLists.size() + ";" + updateResult + "新增：" + insertLists.size() + ";" + addResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【兽医社会化服务组织】定时数据同步任务未开启");
        }
    }

    /**
     * 活畜禽交易市场 (每月1日凌晨35分执行)
     */
    @Scheduled(cron = "0 35 0 1 * ?")
    public void LivestockMarketToDepartment() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.LivestockMarket);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("【活畜禽交易市场】备案定时数据开始同步");
            try {
                List<McQyxxs> l_localList = txdrLivestockMarketService.selectLivestockMarket();
                List<McQyxxs> l_centerList = mcQyxxsService.getCenterQyxxsList(Constant.MC_QYLX_ANIMAL_DEAL_MARKET);
                List<McQyxxs> insertLists = new ArrayList<>();
                List<McQyxxs> updateLists = new ArrayList<>();
                Integer addResult = 0, updateResult = 0;
                if (l_localList != null && l_localList.size() > 0) {
                    for (McQyxxs entity : l_localList) {
                        Optional<McQyxxs> jcqh = l_centerList.stream().filter((McQyxxs bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), QyxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) addResult = mcQyxxsService.batchInsertMcQyxxs(insertLists);
                if (updateLists.size() > 0) updateResult += mcQyxxsService.batchUpdateMcQyxxs(updateLists);
                log.info("【活畜禽交易市场】数据初始化结束:省级平台：" + l_localList.size() + ";农业部数据：" + l_centerList.size() + ";更新：" + updateLists.size() + ";" + updateResult + "新增：" + insertLists.size() + ";" + addResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【活畜禽交易市场】定时数据同步任务未开启");
        }
    }

    /**
     * 动物隔离场备案  (每月1日凌晨40分执行)
     */
    @Scheduled(cron = "0 40 0 1 * ?")
    public void IsolationToDepartment() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.Isolation);
        if (sxAgricultureTask.getZt().intValue() == 1) {
            log.info("【动物隔离场备案】备案定时数据开始同步");
            try {
                List<McQyxxs> l_localList = txdrIsolationService.selectIsolationInfo();
                List<McQyxxs> l_centerList = mcQyxxsService.getCenterQyxxsList(Constant.MC_QYLX_QUARANTINE_STATION_MARKET);
                List<McQyxxs> insertLists = new ArrayList<>();
                List<McQyxxs> updateLists = new ArrayList<>();
                Integer addResult = 0, updateResult = 0;
                if (l_localList != null && l_localList.size() > 0) {
                    for (McQyxxs entity : l_localList) {
                        Optional<McQyxxs> jcqh = l_centerList.stream().filter((McQyxxs bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), QyxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) addResult = mcQyxxsService.batchInsertMcQyxxs(insertLists);
                if (updateLists.size() > 0) updateResult += mcQyxxsService.batchUpdateMcQyxxs(updateLists);
                log.info("【动物隔离场备案】数据初始化结束:省级平台：" + l_localList.size() + ";农业部数据：" + l_centerList.size() + ";更新：" + updateLists.size() + ";" + updateResult + "新增：" + insertLists.size() + ";" + addResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【动物隔离场备案】定时数据同步任务未开启");
        }
    }

    /**
     * 相对人定时同步（屠宰企业）(每月1日凌晨45分执行)
     */
    @Scheduled(cron = "0 45 0 1 * ?")
    public void SlaughterHouseTask() {
        SxAgricultureTask sxAgricultureTask = sxAgricultureTaskService.selectTaskStatus(TaskNameConstant.SlaughterHouse);
        if (sxAgricultureTask != null && sxAgricultureTask.getZt().intValue() == 1) {
            log.info("开始【屠宰企业】定时数据同步");
            try {
                List<McQyxxs> l_localList = txdrSlaughterHouseService.getSlaughterHouseList();
                List<McQyxxs> l_centerList = mcQyxxsService.getCenterQyxxsList(Constant.MC_QYLX_XDR_SLAUGHTER);
                List<McQyxxs> insertLists = new ArrayList<>();
                List<McQyxxs> updateLists = new ArrayList<>();
                Integer addResult = 0, updateResult = 0;
                if (l_localList != null && l_localList.size() > 0) {
                    for (McQyxxs entity : l_localList) {
                        Optional<McQyxxs> jcqh = l_centerList.stream().filter((McQyxxs bean) -> bean.getId().equals(entity.getId())).findFirst();
                        if (jcqh != null && jcqh.isPresent()) {
                            Map compareResult = CommonUtil.compareFields(entity, jcqh.get(), QyxxsComparFileds);
                            if (compareResult != null && compareResult.size() > 0) {
                                updateLists.add(entity);
                            }
                        } else {
                            insertLists.add(entity);
                        }
                    }
                }
                if (insertLists.size() > 0) addResult = mcQyxxsService.batchInsertMcQyxxs(insertLists);
                if (updateLists.size() > 0) updateResult += mcQyxxsService.batchUpdateMcQyxxs(updateLists);
                log.info("【屠宰企业】数据初始化结束:省级平台：" + l_localList.size() + ";农业部数据：" + l_centerList.size() + ";更新：" + updateLists.size() + ";" + updateResult + "新增：" + insertLists.size() + ";" + addResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("【屠宰场】定时数据同步任务未开启");
        }
    }


    //----------------------===============-------------------------





}
